package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zsk
 * @create 2022-03-13-16:02
 */
@DubboService
public class UserServiceImpl implements UserService {

    //依赖注入UserMapper
    @Autowired
    private UserMapper userMapper;


    //保存用户信息
    @Override
    public Long save(User user) {
        //将密码进行md5加密
        String md5 = SecureUtil.md5(user.getPassword());
        user.setPassword(md5);
        //保存用户
        userMapper.insert(user);

        //返回主键id
        return user.getId();
    }

    //查询用户信息
    @Override
    public User findByPhone(String phone) {

        //设置查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getMobile,phone);

        //查询
        return userMapper.selectOne(wrapper);

    }

    //修改手机号
    @Override
    public void update(User user) {
        //调用mapper修改
        userMapper.updateById(user);
    }

    //主键查询
    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }
}
